/**
 * 
 */
package com.nari.baseinfo.action;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.ICity;
import com.nari.baseinfo.model.IFileMapping;
import com.nari.baseinfo.model.IHeatStation;
import com.nari.baseinfo.service.ICityService;
import com.nari.baseinfo.service.IFileMappingService;
import com.nari.baseinfo.service.IHeatStationService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class HeatStationAction extends JSONActionSupport {
	private static final long serialVersionUID = -1697291889203579694L;
	private IHeatStationService iHeatStationService;
	private ICityService iCityService;
	private IFileMappingService iFileMappingService;
	//换热站信息查询
	private String stationName;
	private String districtCityID;
	private String capitalCityID;
	private String provinceCityID;
	private List<ICity> provinceCityList;
	private List<ICity> capitalCityList;
	private List<ICity> districtCityList;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IHeatStation> stationPage;
	private Map<String, String> pageCityMap; //所属城市id-cityName映射表
	//换热站信息管理
	private String stationUID;
	private IHeatStation heatStation;
	//查询换热站列表
	private List<IHeatStation> stationList;
	//安装照片附件
	private IFileMapping attachFileMap;
	private File attachFile;
	private String attachFileFileName;
	
	/**
	 * 分页查询换热站信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String queryStationPage() throws Exception {
		//机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if(provinceCityID!=null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		if(capitalCityID!=null && !"".equals(capitalCityID)) {
			this.districtCityList = this.iCityService.queryCityByParentID(capitalCityID);
		} else {
			this.districtCityList = new ArrayList<ICity>();
		}
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		this.stationPage = this.iHeatStationService.queryStationWithPageCond(stationName, districtCityID, capitalCityID, provinceCityID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.stationPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.stationPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//查询换热站信息对应的行政机构映射表
		this.pageCityMap = this.iHeatStationService.queryReferCitysByStationList(stationPage.getList());
		return "stationList";
	}
	
	/**
	 * 换热站信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String queryStationForManage() throws Exception {
		//换热站信息
		if(this.stationUID!=null && !"".equals(stationUID.trim())) {
			this.heatStation = this.iHeatStationService.queryEntityById(this.stationUID.trim());
			this.provinceCityID = this.heatStation.getProvinceCityID();
			this.capitalCityID = this.heatStation.getCapitalCityID();
			//安装照片
			if(heatStation.getInstallPic()!=null && !"".equals(heatStation.getInstallPic().trim())) {
				this.attachFileMap = this.iFileMappingService.queryEntityById(heatStation.getInstallPic());
			}
		}
		//机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if(provinceCityID!=null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		if(capitalCityID!=null && !"".equals(capitalCityID)) {
			this.districtCityList = this.iCityService.queryCityByParentID(capitalCityID);
		} else {
			this.districtCityList = new ArrayList<ICity>();
		}
		return "stationManage";
	}
	
	/**
	 * 新增或修改换热站信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String saveOrUpdateStation() throws Exception {
		this.rmsg = this.iHeatStationService.saveOrUpdateHeatStation(heatStation, attachFile, attachFileFileName);
		this.heatStationInfoSubmitResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 换热站信息提交响应信息
	 * @param rmsg
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-14
	 */
	private void heatStationInfoSubmitResponse(String rmsg) throws Exception {
		StringBuffer resMsg = new StringBuffer("<script type='text/javascript'>");
		resMsg.append("parent.uploadCallBack('");
		resMsg.append(rmsg);
		resMsg.append("')");
		resMsg.append("</script>");
		this.writeJsonStrToResponse(resMsg.toString());
	}
	
	/**
	 * 异步删除安装照片附件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-13
	 */
	public String deletePicAttachFile() throws Exception {
		this.rmsg = this.iHeatStationService.deleteStationPicAttachFile(stationUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 通过唯一标识删除换热站信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String deleteStationByUID() throws Exception {
		this.rmsg = this.iHeatStationService.deleteHeatStationByUID(stationUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据城市ID查询所属换热站列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-21
	 */
	public String queryStationsByBelongCityID() throws Exception {
		this.stationList = this.iHeatStationService.queryStationByBelongCity(districtCityID, capitalCityID, provinceCityID);
		this.writeJsonStrToResponse(JSONUtil.serialize(stationList));
		return Action.SUCCESS;
	}
	
	/**
	 * @return the iHeatStationService
	 */
	@JSON(serialize=false)
	public IHeatStationService getiHeatStationService() {
		return iHeatStationService;
	}
	
	/**
	 * @param iHeatStationService the iHeatStationService to set
	 */
	public void setiHeatStationService(IHeatStationService iHeatStationService) {
		this.iHeatStationService = iHeatStationService;
	}

	/**
	 * @return the iCityService
	 */
	@JSON(serialize=false)
	public ICityService getiCityService() {
		return iCityService;
	}

	/**
	 * @param iCityService the iCityService to set
	 */
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}

	/**
	 * @return the iFileMappingService
	 */
	@JSON(serialize=false)
	public IFileMappingService getiFileMappingService() {
		return iFileMappingService;
	}

	/**
	 * @param iFileMappingService the iFileMappingService to set
	 */
	public void setiFileMappingService(IFileMappingService iFileMappingService) {
		this.iFileMappingService = iFileMappingService;
	}

	/**
	 * @return the stationName
	 */
	public String getStationName() {
		return stationName;
	}

	/**
	 * @param stationName the stationName to set
	 */
	public void setStationName(String stationName) {
		this.stationName = stationName;
	}

	/**
	 * @return the districtCityID
	 */
	public String getDistrictCityID() {
		return districtCityID;
	}

	/**
	 * @param districtCityID the districtCityID to set
	 */
	public void setDistrictCityID(String districtCityID) {
		this.districtCityID = districtCityID;
	}

	/**
	 * @return the capitalCityID
	 */
	public String getCapitalCityID() {
		return capitalCityID;
	}

	/**
	 * @param capitalCityID the capitalCityID to set
	 */
	public void setCapitalCityID(String capitalCityID) {
		this.capitalCityID = capitalCityID;
	}

	/**
	 * @return the provinceCityID
	 */
	public String getProvinceCityID() {
		return provinceCityID;
	}

	/**
	 * @param provinceCityID the provinceCityID to set
	 */
	public void setProvinceCityID(String provinceCityID) {
		this.provinceCityID = provinceCityID;
	}

	/**
	 * @return the provinceCityList
	 */
	public List<ICity> getProvinceCityList() {
		return provinceCityList;
	}

	/**
	 * @param provinceCityList the provinceCityList to set
	 */
	public void setProvinceCityList(List<ICity> provinceCityList) {
		this.provinceCityList = provinceCityList;
	}

	/**
	 * @return the capitalCityList
	 */
	public List<ICity> getCapitalCityList() {
		return capitalCityList;
	}

	/**
	 * @param capitalCityList the capitalCityList to set
	 */
	public void setCapitalCityList(List<ICity> capitalCityList) {
		this.capitalCityList = capitalCityList;
	}

	/**
	 * @return the districtCityList
	 */
	public List<ICity> getDistrictCityList() {
		return districtCityList;
	}

	/**
	 * @param districtCityList the districtCityList to set
	 */
	public void setDistrictCityList(List<ICity> districtCityList) {
		this.districtCityList = districtCityList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the stationPage
	 */
	public ResultWithPageCond<IHeatStation> getStationPage() {
		return stationPage;
	}

	/**
	 * @param stationPage the stationPage to set
	 */
	public void setStationPage(ResultWithPageCond<IHeatStation> stationPage) {
		this.stationPage = stationPage;
	}

	/**
	 * @return the pageCityMap
	 */
	public Map<String, String> getPageCityMap() {
		return pageCityMap;
	}

	/**
	 * @param pageCityMap the pageCityMap to set
	 */
	public void setPageCityMap(Map<String, String> pageCityMap) {
		this.pageCityMap = pageCityMap;
	}

	/**
	 * @return the stationUID
	 */
	public String getStationUID() {
		return stationUID;
	}

	/**
	 * @param stationUID the stationUID to set
	 */
	public void setStationUID(String stationUID) {
		this.stationUID = stationUID;
	}

	/**
	 * @return the heatStation
	 */
	public IHeatStation getHeatStation() {
		return heatStation;
	}

	/**
	 * @param heatStation the heatStation to set
	 */
	public void setHeatStation(IHeatStation heatStation) {
		this.heatStation = heatStation;
	}

	/**
	 * @return the stationList
	 */
	public List<IHeatStation> getStationList() {
		return stationList;
	}

	/**
	 * @param stationList the stationList to set
	 */
	public void setStationList(List<IHeatStation> stationList) {
		this.stationList = stationList;
	}

	/**
	 * @return the attachFileMap
	 */
	public IFileMapping getAttachFileMap() {
		return attachFileMap;
	}

	/**
	 * @param attachFileMap the attachFileMap to set
	 */
	public void setAttachFileMap(IFileMapping attachFileMap) {
		this.attachFileMap = attachFileMap;
	}

	/**
	 * @return the attachFile
	 */
	public File getAttachFile() {
		return attachFile;
	}

	/**
	 * @param attachFile the attachFile to set
	 */
	public void setAttachFile(File attachFile) {
		this.attachFile = attachFile;
	}

	/**
	 * @return the attachFileFileName
	 */
	public String getAttachFileFileName() {
		return attachFileFileName;
	}

	/**
	 * @param attachFileFileName the attachFileFileName to set
	 */
	public void setAttachFileFileName(String attachFileFileName) {
		this.attachFileFileName = attachFileFileName;
	}
	
}
